﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace peonwar
{
    public class KbHandler
    {
        protected Keys[] lastPressedKeys;
        public string stock = "";
        protected List<Keys> allkey = new List<Keys>();
        public SpriteFont Font;
        public Color Color;
        public int Max;
        protected Vector2 Position;
        protected string bar;
        private string Offset;

        public KbHandler(SpriteFont font, Color color, Vector2 position, int max, string Off)
        {
            lastPressedKeys = new Keys[0];
            this.Position = position;
            this.Color = color;
            this.Font = font;
            this.Max = max;
            this.bar = "";
            this.Offset = Off;
        }

        public virtual void Update(int highticks)
        {
            KeyboardState kbState = Keyboard.GetState();
            Keys[] pressedKeys = kbState.GetPressedKeys();

            foreach (Keys key in pressedKeys)
            {
                char a;
                char.TryParse(key.ToString(), out a);
                
                if (!lastPressedKeys.Contains(key) )
                {
                    if (key == Keys.Back && stock.Length !=0)
                        stock = stock.Substring(0, stock.Length - 1);
                    else
                        if (key == Keys.Space && Font.MeasureString(stock + Offset).X < Max)
                            stock += " ";
                        else if (Font.MeasureString(stock + Offset).X < Max)
                        {
                            #region QWERTY
                            if (!SettingManager.CurrentProfil.azerty)
                            {
                                if (!kbState.IsKeyDown(Keys.LeftShift))
                                {
                                    if (a >= 'A' && a <= 'Z')
                                        stock += (char)(key + 32);
                                    switch (key)
                                    {
                                        case Keys.NumPad0:
                                        case Keys.D0:
                                            stock += '0'; break;
                                        case Keys.NumPad1:
                                        case Keys.D1:
                                            stock += '1'; break;
                                        case Keys.NumPad2:
                                        case Keys.D2:
                                            stock += '2'; break;
                                        case Keys.NumPad3:
                                        case Keys.D3:
                                            stock += '3'; break;
                                        case Keys.NumPad4:
                                        case Keys.D4:
                                            stock += '4'; break;
                                        case Keys.NumPad5:
                                        case Keys.D5:
                                            stock += '5'; break;
                                        case Keys.NumPad6:
                                        case Keys.D6:
                                            stock += '6'; break;
                                        case Keys.NumPad7:
                                        case Keys.D7:
                                            stock += '7'; break;
                                        case Keys.NumPad8:
                                        case Keys.D8:
                                            stock += '8'; break;
                                        case Keys.NumPad9:
                                        case Keys.D9:
                                            stock += '9'; break;
                                        case Keys.Multiply:
                                            stock += '*'; break;
                                        case Keys.Add:
                                            stock += '+'; break;
                                        case Keys.Divide:
                                            stock += '/'; break;
                                        case Keys.Subtract:
                                            stock += '-'; break;
                                        case Keys.OemQuestion:
                                            stock += '/'; break;
                                        case Keys.OemPeriod:
                                            stock += '.'; break;
                                        case Keys.OemComma:
                                            stock += ','; break;
                                        case Keys.OemPlus:
                                            stock += '='; break;
                                        case Keys.OemMinus:
                                            stock += '-'; break;
                                        case Keys.OemOpenBrackets:
                                            stock += '[';break;
                                        case Keys.OemCloseBrackets:
                                            stock += ']';break;
                                        case Keys.OemSemicolon:
                                            stock += ';'; break;
                                        case Keys.OemQuotes:
                                            stock += '\''; break;
                                        case Keys.OemPipe:
                                            stock += '\\'; break;                                            
                                    }
                                }
                                if (kbState.IsKeyDown(Keys.LeftShift))
                                {
                                    if (a >= 'A' && a <= 'Z')
                                        stock += (char)key;
                                    switch (key)
                                    {
                                        case Keys.OemQuestion:
                                            stock += '?'; break;
                                        case Keys.OemPeriod:
                                            stock += '>'; break;
                                        case Keys.OemComma:
                                            stock += '<'; break;
                                        case Keys.D1:
                                            stock += '!'; break;
                                        case Keys.D2:
                                            stock += '@'; break;
                                        case Keys.D3:
                                            stock += '#'; break;
                                        case Keys.D4:
                                            stock += '$'; break;
                                        case Keys.D5:
                                            stock += '%'; break;
                                        case Keys.D6:
                                            stock += '^'; break;
                                        case Keys.D7:
                                            stock += '&'; break;
                                        case Keys.D8:
                                            stock += '*'; break;
                                        case Keys.D9:
                                            stock += '('; break;
                                        case Keys.D0:
                                            stock += ')'; break;
                                        case Keys.OemPlus:
                                            stock += '+'; break;
                                        case Keys.OemMinus:
                                            stock += '_'; break;
                                        case Keys.OemOpenBrackets:
                                            stock += '{'; break;
                                        case Keys.OemCloseBrackets:
                                            stock += '}'; break;
                                        case Keys.OemSemicolon:
                                            stock += ':'; break;
                                        case Keys.OemQuotes:
                                            stock += '\"'; break;
                                        case Keys.OemPipe:
                                            stock += '|'; break;
                                   }
                                }
                            }
                            #endregion

                            #region AZERtY
                            else
                            {
                                if (!kbState.IsKeyDown(Keys.LeftShift))
                                {
                                    if (a >= 'A' && a <= 'Z')
                                        stock += (char)(key + 32);
                                    switch (key)
                                    {
                                        
                                        case Keys.D0:
                                            stock += 'à'; break;
                                        case Keys.D1:
                                            stock += '&'; break;
                                        case Keys.D2:
                                            stock += 'é'; break;
                                        case Keys.D3:
                                            stock += '"'; break;
                                        case Keys.D4:
                                            stock += '\''; break;
                                        case Keys.D5:
                                            stock += '('; break;
                                        case Keys.D6:
                                            stock += '-'; break;
                                        case Keys.D7:
                                            stock += 'è'; break;
                                        case Keys.D8:
                                            stock += '_'; break;
                                        case Keys.D9:
                                            stock += 'ç'; break;
                                        case Keys.Multiply:
                                            stock += '*'; break;
                                        case Keys.Add:
                                            stock += '+'; break;
                                        case Keys.Divide:
                                            stock += '/'; break;
                                        case Keys.Subtract:
                                            stock += '-'; break;
                                        case Keys.OemQuestion:
                                            stock += ':'; break;
                                        case Keys.OemPeriod:
                                            stock += ';'; break;
                                        case Keys.OemComma:
                                            stock += ','; break;
                                        case Keys.OemPlus:
                                            stock += '='; break;
                                        case Keys.OemOpenBrackets:
                                            stock += ')'; break;
                                        case Keys.Oem8:
                                            stock += '!';break;
                                        case Keys.NumPad0:
                                            stock+='0';break;
                                        case Keys.NumPad1:
                                            stock += '1'; break;
                                        case Keys.NumPad2:
                                            stock += '2'; break;
                                        case Keys.NumPad3:
                                            stock += '3'; break;
                                        case Keys.NumPad4:
                                            stock += '4'; break;
                                        case Keys.NumPad5:
                                            stock += '5'; break;
                                        case Keys.NumPad6:
                                            stock += '6'; break;
                                        case Keys.NumPad7:
                                            stock += '7'; break;
                                        case Keys.NumPad8:
                                            stock += '8'; break;
                                        case Keys.NumPad9:
                                            stock += '9'; break;
                                        case Keys.OemCloseBrackets:
                                            stock += '^'; break;
                                        case Keys.OemSemicolon:
                                            stock += '$'; break;
                                        case Keys.OemPipe:
                                            stock += '*'; break;
                                        case Keys.OemBackslash:
                                            stock += '<';break;
                                        case Keys.OemQuotes:
                                            stock += '²';break;
                                        case Keys.OemTilde:
                                            stock += 'ù';break;

                                    }
                                }
                                if (kbState.IsKeyDown(Keys.LeftShift))
                                {
                                    if (a >= 'A' && a <= 'Z')
                                        stock += (char)key;


                                    switch (key)
                                    {
                                        case Keys.OemQuestion:
                                            stock += '/'; break;
                                        case Keys.OemPeriod:
                                            stock += '.'; break;
                                        case Keys.OemComma:
                                            stock += '?'; break;
                                        case Keys.D1:
                                            stock += '1'; break;
                                        case Keys.D2:
                                            stock += '2'; break;
                                        case Keys.D3:
                                            stock += '3'; break;
                                        case Keys.D4:
                                            stock += '4'; break;
                                        case Keys.D5:
                                            stock += '5'; break;
                                        case Keys.D6:
                                            stock += '6'; break;
                                        case Keys.D7:
                                            stock += '7'; break;
                                        case Keys.D8:
                                            stock += '8'; break;
                                        case Keys.D9:
                                            stock += '9'; break;
                                        case Keys.D0:
                                            stock += '0'; break;
                                        case Keys.OemPlus:
                                            stock += '+'; break;
                                        case Keys.Oem8:
                                            stock += '§';break;
                                        case Keys.OemOpenBrackets:
                                            stock += '°';break;
                                        case Keys.OemCloseBrackets:
                                            stock += '¨';break;
                                        case Keys.OemSemicolon:
                                            stock += '£';break;
                                        case Keys.OemPipe:
                                            stock += 'µ';break;
                                        case Keys.OemBackslash:
                                            stock += '>'; break;
                                        case Keys.OemTilde:
                                            stock += '%'; break;
                                    }
                                }
                            }

                        }
                            #endregion
                }
                
            }

            lastPressedKeys = pressedKeys;

            if (highticks % 30 == 0)
            {
                if (bar == "")
                    bar = "|";
                else
                    bar = "";
            }
        }

        public void DrawStock(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(Font, stock + bar, Position, Color);
        }

        public void Reset()
        {
            stock = "";
            
        }

    }
}